<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.queryParams.pageNum = 1; this.pagination.current = 1; this.getList() }"
      @reset="() => { this.queryParams.pageNum = 1; this.pagination.current = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="工法名称" name="name">
            <t-input v-model="queryParams.name" clearable placeholder="请输入工法名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="工法级别" name="level">
            <t-select style="width: 240px" clearable v-model="queryParams.level"
              :options="dict.type.construct_method_level">
            </t-select>
          </t-form-item>
          <t-form-item label="获得时间" name="applyDateRange">
            <t-date-range-picker style="width: 280px" :placeholder="['开始时间', '结束时间']"
              v-model="queryParams.obtainDateRange" allow-input clearable />
          </t-form-item>
          <t-form-item label="填报单位" name="declareDeptName">
            <t-input v-model="queryParams.declareDeptName" clearable placeholder="请输入填报单位" style="width: 240px" />
          </t-form-item>
          <t-form-item label="编制单位" name="declareDeptName">
            <t-input v-model="queryParams.declareDeptName" clearable placeholder="请输入编制单位" style="width: 240px" />
          </t-form-item>
          <t-form-item label="编制人员" name="peopleName">
            <t-input v-model="queryParams.peopleName" clearable style="width: 240px" />
          </t-form-item>
          <t-form-item label="工法状态" name="constructMethodStatus">
            <t-select style="width: 240px" clearable v-model="queryParams.constructMethodStatus"
              :options="dict.type.achievement_status">
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <!-- <div class="btn-group-inline">
      <t-button @click="apply" variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        录入
      </t-button>
    </div> -->
    <t-table rowKey="id" :data="data" :columns="columns" hover :pagination="pagination" :loading="dataLoading">
      <template #name="{ row }">
        <t-link @click="dialog.objId = row.id; dialog.header = row.name; dialog.open_review = true" hover="color"
          theme="primary">{{ row.name }}</t-link>
      </template>
      <template #memberList="{ row }">
        <template v-for="(item, index) in row.memberList">
          <a class="t-button-link" :key="index">{{ item.name }}</a>{{ index + 1 === row.memberList.length ? '' : '、' }}
        </template>
      </template>
      <template #companyList="{ row }">
        <template v-for="(item, index) in row.companyList">
          <a class="t-button-link" :key="index">{{ item.company }}</a>{{ index + 1 === row.companyList.length ? '' : '、' }}
        </template>
      </template>
      <template #level="{ row }">
        {{ row.level | filterByDict(dict.type.construct_method_level) }}
      </template>
      <template #constructMethodStatus="{ row }">
        <t-tag @click="dialog.header = row.name; dialog.objId = row.id; dialog.open_approve_list = true" class="cursor-pointer"
          :theme="handleTheme(row.constructMethodStatus, dict.type.achievement_status)" variant="light">
          {{ row.constructMethodStatus | filterByDict(dict.type.achievement_status) }}
        </t-tag>
      </template>
      <template #op="{ row }">
        <!--        v-if="['1','4',null].includes(row.gfStatus)"-->
        <t-button @click="updateData(row)" size="small" variant="text" class="t-button-link"
          v-if="row.createUserId === Number($store.state.user.userId)">
          <t-icon name="edit-1" slot="icon" />
          继续录入
        </t-button>
        <t-button size="small" @click="dialog.objId = row.id; dialog.header = row.name; dialog.open_detail = true"
          variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
        <!--        ['1','4',null].includes(row.gfStatus)  &&-->
        <t-button v-if="row.createUserId === Number($store.state.user.userId)" theme="danger" @click="delObj(row)"
          size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon" />
          删除
        </t-button>
      </template>
    </t-table>
    <gf-detail-dialog :objId="dialog.objId" :header="dialog.header" :visible.sync="dialog.open_detail"
      v-if="dialog.open_detail">
    </gf-detail-dialog>
    <file-review :header="dialog.header" achievementType="7" :visible.sync="dialog.open_review"
      v-if="dialog.open_review" :objId="dialog.objId"></file-review>

  </div>
</template>

<script>
import GfDetailDialog from "./components/gf-detail-dialog.vue";
import fileReview from "../components/file-review.vue";


export default {
  name: "gf-my",
  dicts: ['construct_method_level', 'achievement_status'],
  components: {
    GfDetailDialog,
    fileReview
  },
  data () {
    return {
      dialog: {
        header: "",
        open: false,
        objId: null,
        open_approve_list: false,
        open_detail: false,
        open_review: false,
      },
      data: [],
      columns: [
        {
          colKey: "name",
          title: "工法名称",
          align: "center",
        },
        {
          colKey: "level",
          title: "工法级别",
          align: "center",
        },
        {
          colKey: "obtainDate",
          title: "获得时间",
          align: "center",
        },
        {
          colKey: "companyList",
          title: "编制单位",
          align: "center",
        },
        {
          colKey: "memberList",
          title: "编制人员",
          align: "center",
        },
        {
          colKey: "constructMethodStatus",
          title: "工法状态",
          align: "center",
          fixed: 'right'
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 100
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        level: '',
        obtainDateRange: [],
        peopleName: '',
        declareDeptName: '',
        constructMethodStatus: '',
        startObtainDate: '',
        endObtainDate: '',
        businessId: null,

      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created () {
    this.getList();
  },
  methods: {
    getList () {
      this.dataLoading = true;
      [this.queryParams.startObtainDate, this.queryParams.endObtainDate] = this.queryParams.obtainDateRange;
      this.$api.cgzh.gf.myList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    updateData (row) {
      sessionStorage.removeItem(`${row.id}-gf-activeForm`)
      const taskId = row.id;
      const activeFormList = [];
      this.$api.cgzh.gf.getAll(row.id).then(response => {
        sessionStorage.removeItem(`${taskId}-gf-gfId`);
        if (response.data.id) {
          sessionStorage.setItem(`${taskId}-gf-gfId`, response.data.id)
        } else {
          activeFormList.push(0);
        }
        if (response.data.constructMethodPreface && response.data.constructMethodPreface.id) {
          sessionStorage.setItem(`${taskId}-gf-prefaceId`, response.data.constructMethodPreface.id)
        } else {
          activeFormList.push(1);
        }
        if (response.data.constructMethodFeature && response.data.constructMethodFeature.id) {
          sessionStorage.setItem(`${taskId}-gf-featureId`, response.data.constructMethodFeature.id)
        } else {
          activeFormList.push(2);
        }
        if (response.data.constructMethodScope && response.data.constructMethodScope.id) {
          sessionStorage.setItem(`${taskId}-gf-scopeId`, response.data.constructMethodScope.id)
        } else {
          activeFormList.push(3);
        }
        if (response.data.constructMethodPrinciple && response.data.constructMethodPrinciple.id) {
          sessionStorage.setItem(`${taskId}-gf-principleId`, response.data.constructMethodPrinciple.id)
        } else {
          activeFormList.push(4);
        }
        if (response.data.constructMethodProcess && response.data.constructMethodProcess.id) {
          sessionStorage.setItem(`${taskId}-gf-processId`, response.data.constructMethodProcess.id)
        } else {
          activeFormList.push(5);
        }
        if (response.data.constructMethodEquipment && response.data.constructMethodEquipment.id) {
          sessionStorage.setItem(`${taskId}-gf-equipmentId`, response.data.constructMethodEquipment.id)
        } else {
          activeFormList.push(6);
        }
        if (response.data.constructMethodQuality && response.data.constructMethodQuality.id) {
          sessionStorage.setItem(`${taskId}-gf-qualityId`, response.data.constructMethodQuality.id)
        } else {
          activeFormList.push(7);
        }
        if (response.data.constructMethodMeasure && response.data.constructMethodMeasure.id) {
          sessionStorage.setItem(`${taskId}-gf-measureId`, response.data.constructMethodMeasure.id)
        } else {
          activeFormList.push(8);
        }
        if (response.data.constructMethodEnvironment && response.data.constructMethodEnvironment.id) {
          sessionStorage.setItem(`${taskId}-gf-environmentId`, response.data.constructMethodEnvironment.id)
        } else {
          activeFormList.push(9);
        }
        if (response.data.constructMethodBenefit && response.data.constructMethodBenefit.id) {
          sessionStorage.setItem(`${taskId}-gf-benefitId`, response.data.constructMethodBenefit.id)
        } else {
          activeFormList.push(10);
        }
        if (response.data.constructMethodApplication && response.data.constructMethodApplication.id) {
          sessionStorage.setItem(`${taskId}-gf-applicationId`, response.data.constructMethodApplication.id)
        } else {
          activeFormList.push(11);
        }
        if (response.data.projectList && response.data.projectList.length > 0) {
          sessionStorage.setItem(`${taskId}-gf-projectId`, response.data.projectList.length)
        } else {
          activeFormList.push(12);
        }
        if (activeFormList.length <= 0) {
          sessionStorage.setItem(`${taskId}-gf-activeForm`, 13); // 跳转到最后一步
        } else {
          sessionStorage.setItem(`${taskId}-gf-activeForm`, activeFormList[0]); // 跳转到第一个不满足
        }
        this.$router.push({ path: `/cgzh/other/gf/gf-apply/${row.id}`, query: { title: `工法录入-${row.name}` } });
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    apply () {
      this.$router.push(`/cgzh/other/gf/gf-apply/${Date.parse(new Date())}`);
    },
    delObj (row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除工法名称为"${row.name}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          this.$api.cgzh.gf.delete(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            this.$message.error(e.toString());
          });
        }
      });
    }
  }
}
</script>

<style scoped></style>
